const tagsView = {
  state: {
    visitedViews: [],
    cachedViews: []
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      // tabs 有id为详情，没id为新增
      if (view.query.hasOwnProperty('id') || view.params.hasOwnProperty('id')) {
        view.meta.title = view.meta.title.replace('新增', '详情')
      } else {
        view.meta.title = view.meta.title.replace('详情', '新增')
      }
      if (state.visitedViews.some(v => v.name === view.name)) {
        state.visitedViews.map((value) => {
          if (value.name === view.name) {
            value.query = view.query
            value.params = view.params
            value.path = view.path
            value.title = view.meta.title
            value.name = view.name
          }
        })
        return
      }
      let obj = {
        path: view.path,
        query: view.query,
        params: view.params,
        title: view.meta.title || 'no-name',
        name: view.name
      }
      state.visitedViews.push(obj)
      if (!view.meta.noCache) {
        state.cachedViews.push(view.name)
      }
    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
    },
    DEL_CURRENT_VIEWS: (state, route) => {
      let view = {
        path: route.path,
        query: route.query,
        title: route.meta.title,
        name: route.name
      }
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews = state.cachedViews.slice(index, i + 1)
          break
        }
      }
    },
    DEL_ALL_VIEWS: (state) => {
      state.visitedViews = []
      state.cachedViews = []
    },
    INIT_VISITED_VIEWS: (state) => {
      state.visitedViews = JSON.parse(sessionStorage.getItem('tagsView') || '{}').visitedViews || [{
        'path': '/',
        'title': '欢迎页',
        'name': 'welcome'
      }]
      state.cachedViews = JSON.parse(sessionStorage.getItem('tagsView') || '{}').cachedViews || ['welcome']
    },
    // 拖拽后设置
    SET_VISITED_VIEWS: (state, data) => {
      state.visitedViews = data
      state.cachedViews = data.reduce((prev, curr) => [...prev, curr.name], [])
    }
  },
  actions: {
    initVisitedViews({commit}, view) {
      commit('INIT_VISITED_VIEWS', view)
    },
    addVisitedViews({commit}, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({commit, state}, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    /**
     * 关闭当前页面：页面调用如下
     this.$store.dispatch('delCurrentViews', this.$route).then(
     // 关闭当前页面后的逻辑，如跳转到列表页面
     this.$router.push('/***List')
     )
     * @param commit
     * @param state
     * @param route 传入页面中的this.$route
     * @returns {Promise<any>}
     */
    delCurrentViews({commit, state}, route) {
      return new Promise((resolve) => {
        commit('DEL_CURRENT_VIEWS', route)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews({commit, state}, view) {
      return new Promise((resolve) => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews({commit, state}) {
      return new Promise((resolve) => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    }
  }
}

export default tagsView
